package com.antu.first.service.impl;

import com.antu.first.bean.DiscountCoupon;
import com.antu.first.bean.Hotel;
import com.antu.first.bean.Merchant;
import com.antu.first.bean.User;
import com.antu.first.dao.DiscountCouponDao;
import com.antu.first.dto.DiscountCouponExecution;
import com.antu.first.enums.DiscountCouponEnum;
import com.antu.first.exceptions.DiscountCouponException;
import com.antu.first.service.DiscountCouponService;
import com.antu.first.service.MerchantLogionService;
import com.antu.first.util.PageCaculator;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("discountCouponService")
public class DiscountCouponServiceImpl implements DiscountCouponService{
    @Autowired
    private DiscountCouponDao discountCouponDao;
    @Autowired
    private MerchantLogionService merchantLogionService;
    Map<String,Object> map=new HashMap<String,Object>();
    int pageSize=10;//设置分页条数为10
    @Override
    public DiscountCouponExecution queryAllsByUid(int uId, int canUse, int pageIndex) {
        try{
            int count = discountCouponDao.countAllsByUid(uId, canUse);
            int rowIndex = 0;
            if((count/10)+1 >= pageIndex){
                rowIndex = PageCaculator.caculateRowIndex(pageIndex, 10);
                List<DiscountCoupon> discountCoupons = discountCouponDao.queryAllsByUid(uId, canUse, rowIndex);
                if(discountCoupons.size() > 0){
                    DiscountCouponExecution execution = new DiscountCouponExecution(DiscountCouponEnum.QUERY_SUCCESS, discountCoupons);
                    execution.setCount(count);
                    return execution;
                }else{
                    return new DiscountCouponExecution(DiscountCouponEnum.QUERY_FAILED);
                }
            }else{
                return new DiscountCouponExecution(DiscountCouponEnum.IS_LAST);
            }
        }catch(Exception e){
            throw new DiscountCouponException("DiscountCouponServiceImpl-queryAllsByUid，查询用户优惠券失败："+e.getMessage());
        }
    }

    @Override
    public Map<String,Object> addDiscountCoupon(DiscountCoupon discountCoupon) throws Exception{
        int n=discountCouponDao.addDiscountCoupon(discountCoupon);
        /*1表添加失败 2表添加成功*/
        if(n>0){
            map.put("success",true);
            map.put("message","添加成功");
        }else{
            map.put("success",false);
            map.put("message","信息有误，添加失败");
        }
        return map;
    }

    @Override
    public Map<String,Object> selectDiscountCouponByValue(DiscountCoupon discountCoupon) throws Exception{
        List<DiscountCoupon> list=discountCouponDao.selectDiscountCouponByValue(discountCoupon);
        if (list!=null){
            map.put("allRecord",list.size());
            map.put("allPage",Math.ceil(list.size()/pageSize));
            map.put("list",list);
        }else {
            map.put("message","查询优惠券失败");
        }
        return map;
    }

    @Override
    public Map<String,Object> selectDiscountCouponByDate(DiscountCoupon discountCoupon) throws Exception{
        List<DiscountCoupon> list=discountCouponDao.selectDiscountCouponByDate(discountCoupon);
        if(list!=null){
            map.put("allRecord",list.size());
            map.put("allPage",Math.ceil(list.size()/pageSize));
            map.put("list",list);
        }else {
            map.put("message","根据时间区间查询优惠券失败");
        }
        return map;
    }

    @Override
    public Map<String,Object> selectDiscountCouponDateByUnuse(DiscountCoupon discountCoupon) throws Exception{
        List<DiscountCoupon> list1=discountCouponDao.selectDiscountCouponUnuse(discountCoupon);
        List<DiscountCoupon> list4=null;
        for (DiscountCoupon  dis:list1) {
            int dId=dis.getdId();
            int n=discountCouponDao.updateDiscountCouponUnuseState1(dId);
            if(n>0){
                list4=discountCouponDao.selectDiscountCouponUnuseByState(discountCoupon);
                if(list4!=null){
                    map.put("allRecord",list4.size());
                    map.put("allPage",Math.ceil(list4.size()/pageSize));
                    map.put("list",list4);
                }else {
                    map.put("message","未生效优惠券查询异常");
                }
            }else{
                map.put("message","未生效优惠券状态码异常");
            }
        }
        return map;
    }

    @Override
    public Map<String,Object> selectDiscountCouponDateByUsing(DiscountCoupon discountCoupon) throws Exception{
        List<DiscountCoupon> list2=discountCouponDao.selectDiscountCouponUsing(discountCoupon);
        List<DiscountCoupon> list=null;
        for (DiscountCoupon  dis:list2) {
            int dId=dis.getdId();
            int n=discountCouponDao.updateDiscountCouponUsingState2(dId);
            if(n>0){
                list=discountCouponDao.selectDiscountCouponUsingByState(discountCoupon);
                if(list!=null){
                    map.put("allRecord",list.size());
                    map.put("allPage",Math.ceil(list.size()/pageSize));
                    map.put("list",list);
                }else {
                    map.put("message","生效中优惠券查询异常");
                }
            }else{
                map.put("message","生效中优惠券状态码异常");
            }
        }
        return map;
    }

    @Override
    public Map<String,Object> selectDiscountCouponDateByUsed(DiscountCoupon discountCoupon) {
        List<DiscountCoupon> list3=discountCouponDao.selectDiscountCouponUsed(discountCoupon);
        List<DiscountCoupon> list2=null;
        for (DiscountCoupon  dis:list3) {
            int dId=dis.getdId();
            int n=discountCouponDao.updateDiscountCouponUsedState3(dId);
            if(n>0){
                list2=discountCouponDao.selectDiscountCouponUsedByState(discountCoupon);
                if(list2!=null){
                    map.put("allRecord",list2.size());
                    map.put("allPage",Math.ceil(list2.size()/pageSize));
                    map.put("list",list2);
                }else {
                    map.put("message","未生效优惠券查询异常");
                }
            }else{

            }map.put("message","未生效优惠券状态码异常");
        }
        return map;
    }

    @Override
    public Map<String, Object> updateDiscountCouponInfor(DiscountCoupon discountCoupon) throws Exception {
        int n=discountCouponDao.updateDiscountCouponInfor(discountCoupon);
        if(n>0){
            map.put("success",true);
            map.put("message","信息修改成功");
        }else {
            map.put("success",false);
            map.put("message","信息填写错误，修改失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteDiscountCouponById(DiscountCoupon discountCoupon) throws Exception {
        int n=discountCouponDao.deleteDiscountCouponById(discountCoupon);
        if(n>0){
            map.put("success",true);
            map.put("message","优惠券删除成功");
        }else {
            map.put("success",false);
            map.put("message","系统繁忙，删除失败，请稍后再试");
        }
        return map;
    }

    @Override
    public Map<String,Object> selectAllDiscountCoupon(HttpServletRequest request,int pageIndex,int pageSize) throws Exception {
        Merchant merchant1=(Merchant) request.getSession().getAttribute("merchant");
        Hotel hotel=new Hotel();
        hotel.sethId(merchant1.getHotel().gethId());
        if (pageIndex==0){
            pageIndex=0;
        }
        List<DiscountCoupon> list=discountCouponDao.selectAllDiscountCoupon(hotel,pageIndex-1,pageSize);
        List<DiscountCoupon> list1=discountCouponDao.selectAllDiscountCouponNoLimit(hotel);
        if (list1 != null) {
            if (list != null) {
                if (list1.size() % pageSize == 0) {
                    map.put("nowPageNum", pageSize);
                } else {
                    map.put("nowPageNum", list1.size() % pageSize);
                }
                map.put("allPage", Math.ceil(list1.size() / pageSize));
                map.put("allRecord", list1.size());
                map.put("list", list);
                map.put("success", true);
            } else {
                map.put("success", false);
                map.put("message", "优惠券查询失败");
            }
        }else {
            map.put("success", false);
            map.put("message", "优惠券查询失败");
        }
        return map;
    }

    /**
     * 用户查询与自己相关的优惠券
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> usersGetAlTheCoupons(User user,Integer status) {
        user.setuId(1);
        Map<String,Object> usermap=new HashMap<String,Object>();
        List<DiscountCoupon> list=discountCouponDao.usersGetAlTheCoupons(user,status);
        usermap.put("allDiscountCoupon",list);
        return usermap;
    }

    @Override
    public Map<String, Object> userRobStamps(DiscountCoupon discountCoupon) {
        Map<String,Object> usermap=new HashMap<String,Object>();
        if (discountCoupon.getUser()!=null){
            int i=discountCouponDao.checkNumebr(discountCoupon); //先检查优惠券剩余数量
            if (i>0){
                discountCoupon.setdCount(i-1);//让总数减一
                discountCoupon.setdStatus(2);//设置状态
                int j= discountCouponDao.userRobStamps(discountCoupon);//抢券
                int k=discountCouponDao.numberOfSynchronous(discountCoupon);//同步数量
                if (j>0&&k>0){
                    usermap.put("success",true);
                }else {
                    usermap.put("success",false);
                    usermap.put("errMsg","网络错误，请重试");
                }
            }else {
                usermap.put("errMsg","手太慢了，券被抢完了；下次再来吧");
                usermap.put("success",false);
            }
        }else {
            usermap.put("errMsg","未获取到用户信息");
            usermap.put("success",false);
        }

        return usermap;
    }

    @Override
    public DiscountCoupon queryDiscountCouponById(int id) {

        return discountCouponDao.queryCouponById(id);
    }
}
